package com.example.stumanager.admin;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.stumanager.R;
import com.example.stumanager.database.UserSql;
import com.example.stumanager.entity.UserEntity;

/**
 * 添加学生
 */
public class AdminAddStudentsPage extends AppCompatActivity {
    EditText number, name, passWord, dormitory, grade;
    Button add,c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_add_students_page);

        c=findViewById(R.id.re);
        add=findViewById(R.id.add);

        number = findViewById(R.id.number);
        name = findViewById(R.id.name);
        passWord = findViewById(R.id.passWord);
        grade = findViewById(R.id.grade);
        dormitory = findViewById(R.id.dormitory);

        UserSql db=new UserSql(this);
        c.setOnClickListener(view -> finish());
        add.setOnClickListener(view -> {
            if(number.getText().toString().equals("") ||
                    passWord.getText().toString().equals("") ||
                    name.getText().toString().equals("")||grade.getText().toString().equals("")||dormitory.getText().toString().equals("") ){
                Toast.makeText(AdminAddStudentsPage.this, "信息不能为空", Toast.LENGTH_SHORT).show();
                return ;
            }
                UserEntity u=new UserEntity(number.getText().toString(), name.getText().toString(), passWord.getText().toString(),
                        dormitory.getText().toString(), grade.getText().toString(),1);
                if(db.add(u)){
                    Toast.makeText(AdminAddStudentsPage.this, "添加成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else{
                    Toast.makeText(AdminAddStudentsPage.this, "添加失败，此学号已存在", Toast.LENGTH_SHORT).show();
                }
        });
    }
}